﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAntLibrary.NDEntities;
using System.Xml;
using System.IO;
using MAntLibrary.Utility;
using System.Reflection;

namespace MAntLibrary.suppliers
{
    public class Heureka
    {
        public static Result ReadXML(FileInfo file, ref List<ND_Commodity> listofcommodities, ref List<ND_Category> listofcategories)
        {
            Result result = new Result();
            ND_Commodity zbozi;

            XmlDocument doc = new XmlDocument();
            XmlNode root = null;

            try { doc.Load(file.FullName); }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                result.ResultCode = ResultCode.ERR;
                return result;
            }

            // ziskani korenoveho elementu
            root = doc.DocumentElement;

            // pruchod vnorenymi uzly korenoveho elementu
            foreach (XmlNode lNode in root.ChildNodes)
            {
                zbozi = new ND_Commodity();
                if (lNode.Name.Equals("SHOPITEM"))
                {
                    XmlNodeList deti = lNode.ChildNodes;
                    foreach (XmlNode d in deti)
                    {
                        if (d.Name.Equals("ITEM_ID")) { zbozi.IdZbozi = d.InnerText; continue; }
                        if (d.Name.Equals("PRODUCT")) { zbozi.JmenoVyrobku = d.InnerText; continue; }
                        if (d.Name.Equals("DESCRIPTION"))
                        {
                            zbozi.Popis = d.InnerText;
                            zbozi.KratkyPopis = d.InnerText;
                            continue;
                        }
                        
                      
                        if (d.Name.Equals("IMGURL")) { zbozi.PictureList.Add(new ND_Picture(d.InnerText, d.InnerText)); continue; }
                        if (d.Name.Equals("PRICE_VAT")) { zbozi.CenaSDPH = d.InnerText; continue; }
                        if (d.Name.Equals("CATEGORYTEXT"))
                        {
                            // OLD
                            //string[] tmp = d.InnerText.Split('|');
                            //zbozi.Category = new ND_Category(0, string.Empty, string.Empty, "0", tmp[tmp.Length-1], tmp[tmp.Length-1]);
                            //for (int i = 0; i < tmp.Length; i++)
                            //{
                            //    ND_Category C = new ND_Category(i, string.Empty, string.Empty, "0", tmp[i], tmp[i]);
                            //    if (!ND_Category.IsExist(C, categoryList))
                            //    {
                            //        categoryList.Add(C);
                            //        Log.writeInfo(MethodBase.GetCurrentMethod(), C.JmenoKategorie + ", uroven: " +C.Uroven);
                            //    }
                            //}

                            // NEW
                            string[] tmp = d.InnerText.Split('|');
                            ND_Category cat;
                            foreach (string s in tmp)
                            {
                                cat = new ND_Category(-1, "-1", "-1", "0", s, s);
                                if (ND_Category.IsExist(cat, listofcategories));
                                {
                                    listofcategories.Add(cat);
                                }
                                zbozi.Category = cat;
                            }
                            continue;
                        }
                    }
                }
                listofcommodities.Add(zbozi);
            }
            SetCategoryId(ref listofcommodities, ref listofcategories);
            return result;
        }

        private static void SetCategoryId(ref List<ND_Commodity> listofcommodities, ref List<ND_Category> listofcategories)
        {
            foreach (ND_Commodity comm in listofcommodities)
            {
                listofcommodities ma nejak moc prvku
                foreach (ND_Category catt in listofcategories)
                {
                    if (catt.JmenoKategorie.Equals(comm.Category.JmenoKategorie))
                    {
                        comm.Category = catt;
                        break;
                    }
                }
            }
        }
    }
}


